﻿using Company.Core.Enums;
using Company.Core.Helpers;
using Company.Core.Interfaces;
using Company.Core.Logger;
using System.IO;

namespace Company.Shell.Services
{
    class ConfigManager : IConfigManager
    {
        private const string root = "Config";
        private string GetFullPath(ConfigType configType)
        {
            return Path.Combine(root, configType.GetType().Name + "." + configType.ToString() + ".json");
        }
        public T Read<T>(ConfigType type) where T : class
        {
            string filename=GetFullPath(type);
            return JsonHelper.Load<T>(filename);
        }

        public bool Write<T>(ConfigType type, T value)
        {
            try
            {
                Directory.CreateDirectory(root);
                string filename = GetFullPath(type);
                JsonHelper.Save(value, filename);
                return true;
            }
            catch (Exception ex)
            {
                Logs.Error(ex);
                return false;
            }
        }
    }
}
